package com.zb.knowledge.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.zb.entity.ZjKnowledgeType;
import com.zb.extend.*;
import com.zb.knowledge.service.ConKnowledgeTypeService;
import com.zb.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author WEN
 * @Date 2023/11/13 0013
 **/
@Service
public class ConKnowledgeTypeServiceImpl implements ConKnowledgeTypeService {
    @Resource
    private ZjKnowledgeTypeInstructionBookService knowledgeTypeInstructionBookService;
    @Resource
    private ZjKnowledgeTypeVideoService knowledgeTypeVideoService;
    @Resource
    private ZjKnowledgeTypeFaultService knowledgeTypeFaultService;
    @Resource
    private ZjKnowledgeTypeService knowledgeTypeService;
    @Resource
    private ZjProductKnowledgeTypeService productKnowledgeTypeService;
    @Resource
    private ZjUserTypeKnowledgeTypeService userTypeKnowledgeTypeService;

    // 批量删除知识库分类
    @Override
    public void deleteBatchById(List<Long> ids) {
        List<KnowledgeTypeInstructionBookExtend> extendList = knowledgeTypeInstructionBookService.getByKnowledgeTypeIds(ids);
        if (CollectionUtil.isNotEmpty(extendList)) {
            exceptionMessage(extendList.stream().map(KnowledgeTypeInstructionBookExtend::getKnowledgeTypeId).distinct().toList(),
                    "说明书", extendList.stream().map(KnowledgeTypeInstructionBookExtend::getName).distinct().collect(Collectors.joining("、")));
        }
        List<KnowledgeTypeVideoExtend> videoExtendList = knowledgeTypeVideoService.getByKnowledgeTypeIds(ids);
        if (CollectionUtil.isNotEmpty(videoExtendList)) {
            exceptionMessage(videoExtendList.stream().map(KnowledgeTypeVideoExtend::getKnowledgeTypeId).distinct().toList(),
                    "视频", videoExtendList.stream().map(KnowledgeTypeVideoExtend::getName).distinct().collect(Collectors.joining("、")));
        }
        List<KnowledgeTypeFaultExtend> faultExtendList = knowledgeTypeFaultService.getByKnowledgeTypeIds(ids);
        if (CollectionUtil.isNotEmpty(faultExtendList)) {
            exceptionMessage(faultExtendList.stream().map(KnowledgeTypeFaultExtend::getKnowledgeTypeId).distinct().toList(),
                    "故障库", faultExtendList.stream().map(KnowledgeTypeFaultExtend::getName).distinct().collect(Collectors.joining("、")));
        }
        List<ProductKnowledgeTypeExtend> productKnowledgeTypeExtendList = productKnowledgeTypeService.getByKnowledgeTypeIds(ids);
        if (CollectionUtil.isNotEmpty(productKnowledgeTypeExtendList)) {
            exceptionMessage(productKnowledgeTypeExtendList.stream().map(ProductKnowledgeTypeExtend::getKnowledgeTypeId).distinct().toList(),
                    "产品", productKnowledgeTypeExtendList.stream().map(e -> {
                        return e.getName() + "（" + e.getModel() + "）";
                    }).distinct().collect(Collectors.joining("、")));
        }
        List<UserTypeKnowledgeTypeExtend> userTypeKnowledgeTypeExtendList = userTypeKnowledgeTypeService.getByKnowledgeTypeIds(ids);
        if (CollectionUtil.isNotEmpty(userTypeKnowledgeTypeExtendList)) {
            exceptionMessage(userTypeKnowledgeTypeExtendList.stream().map(UserTypeKnowledgeTypeExtend::getKnowledgeTypeId).distinct().toList(),
                    "客户分类", userTypeKnowledgeTypeExtendList.stream().map(UserTypeKnowledgeTypeExtend::getUserType).distinct().collect(Collectors.joining("、")));
        }
        // TODO 服务商关联
        // TODO 服务商人员关联
        knowledgeTypeService.removeByIds(ids);
    }

    private void exceptionMessage(List<Long> ids, String message, String name) {
        QueryWrapper<ZjKnowledgeType> typeQueryWrapper = new QueryWrapper<>();
        typeQueryWrapper.select("name");
        typeQueryWrapper.in("id", ids);
        String names = knowledgeTypeService.list(typeQueryWrapper).stream().map(ZjKnowledgeType::getName).collect(Collectors.joining("、"));
        Assert.isTrue(false,
                "知识库分类名称 ["+ names+"] 的数据已与"+ message +" ["+ name +"] 关联使用，不允许删除");
    }
}
